/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.interceptor;

import com.minierp.bean.UserBean;
import com.minierp.bean.UserStatus;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.EncInterceptorHelper;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class UserSignonInterceptor extends HandlerInterceptorAdapter {
	private String loginView;

	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		UserStatus s = EncInterceptorHelper.checkSignon(request);
		UserBean ub = s.getUserBean();
		if (ub == null) {
			String url = request.getRequestURI();
			String query = request.getQueryString();
			ModelAndView mv = new ModelAndView("redirect:" + this.loginView);
			if (query != null)
				mv.addObject("signonForwardUrl", url + "?" + query);
			else {
				mv.addObject("signonForwardUrl", url);
			}
			EncCommonHelper.setAlertMessage(request, s.getAlertMsg());
			throw new ModelAndViewDefiningException(mv);
		}
		return true;
	}

	public String getLoginView() {
		return this.loginView;
	}

	public void setLoginView(String loginView) {
		this.loginView = loginView;
	}
}